package com.houweishi.module.agentdevice.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.houweishi.common.annotation.DataSource;
import com.houweishi.common.enums.DataSourceType;
import com.houweishi.common.utils.DateUtils;
import com.houweishi.module.agentaddress.domain.AgentAddress;
import com.houweishi.module.agentaddress.mapper.AgentAddressMapper;
import com.houweishi.module.agentdevice.domain.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.houweishi.module.agentdevice.mapper.AgentDeviceMapper;
import com.houweishi.module.agentdevice.domain.AgentDevice;
import com.houweishi.module.agentdevice.service.IAgentDeviceService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 设备管理Service业务层处理
 * 
 * @author houweishi
 * @date 2023-03-10
 */
@Service
public class AgentDeviceServiceImpl implements IAgentDeviceService 
{
//    @Autowired
    @Resource
    private AgentDeviceMapper agentDeviceMapper;

    @Resource
    private AgentAddressMapper agentAddressMapper;

    /**
     * 查询设备管理
     * 
     * @param deviceId 设备管理主键
     * @return 设备管理
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public AgentDevice selectAgentDeviceByDeviceId(Long deviceId)
    {
        return agentDeviceMapper.selectAgentDeviceByDeviceId(deviceId);
    }

    /**
     * 查询设备管理列表
     * 
     * @param agentDevice 设备管理
     * @return 设备管理
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public List<AgentDevice> selectAgentDeviceList(AgentDevice agentDevice)
    {
        List<AgentDevice> list = agentDeviceMapper.selectAgentDeviceList(agentDevice);
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(device->{
                AgentAddress address = agentAddressMapper.selectAgentAddressById(device.getAddressId());
                device.setProvince(address.getProvince());
                device.setCity(address.getCity());
                device.setDistrict(address.getDistrict());
                if(address.getStreet()!=null){
                    device.setStreet(address.getStreet());}
            });

        }

        return list;
    }

    /**
     * 新增设备管理
     * 
     * @param agentDevice 设备管理
     * @return 结果
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public int insertAgentDevice(AgentDevice agentDevice)
    {
        agentDevice.setCreateTime(DateUtils.getNowDate());
        return agentDeviceMapper.insertAgentDevice(agentDevice);
    }

    /**
     * 修改设备管理
     * 
     * @param agentDevice 设备管理
     * @return 结果
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public int updateAgentDevice(AgentDevice agentDevice)
    {
        return agentDeviceMapper.updateAgentDevice(agentDevice);
    }

    /**
     * 批量删除设备管理
     * 
     * @param deviceIds 需要删除的设备管理主键
     * @return 结果
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public int deleteAgentDeviceByDeviceIds(Long[] deviceIds)
    {
        return agentDeviceMapper.deleteAgentDeviceByDeviceIds(deviceIds);
    }

    /**
     * 删除设备管理信息
     * 
     * @param deviceId 设备管理主键
     * @return 结果
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public int deleteAgentDeviceByDeviceId(Long deviceId)
    {
        return agentDeviceMapper.deleteAgentDeviceByDeviceId(deviceId);
    }

    /**
     * 查询设备管理列表
     *
     * @param agentDevice 设备管理
     * @return 设备管理
     */
    @DataSource(value = DataSourceType.SLAVE1)
    @Override
    public List<AddressVo> selectAgentDeviceList2(AgentDevice agentDevice)
    {
        List<AddressVo> addressVoList = new ArrayList<>();

        AgentDevice agentDevice1 = new AgentDevice();
        agentDevice1.setDeviceStatus("y");

        List<AgentDevice> list = agentDeviceMapper.selectAgentDeviceList(agentDevice1);
        list.forEach(device->{

            if(device.getAddressId()!=null) {
                AddressVo addressVo = new AddressVo();
//                AgentAddress address = agentAddressMapper.selectAgentAddressById(device.getAddressId());
//                if(address.getDetail()!=null){
                addressVo.setDeviceId(device.getDeviceId());
                addressVo.setDeviceName(device.getDeviceName());
                addressVo.setDeviceCode(device.getDeviceCode());
//                addressVo.setDetailAddress(address.getDetail());
                addressVo.setLongitude(device.getLongitude());
                addressVo.setLatitude(device.getLatitude());
                addressVoList.add(addressVo);
            }
        });

        return addressVoList;
    }
}
